package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import pojo.Order;
import util.DBUtils;
import dao.OrderDao;

public class OrderDaoImpl extends BaseDaoImpl<Order> implements OrderDao {

	@Override
	public int add(Order order) {
		String sql = "insert into orders values(?,?,?,?,?)";
		PreparedStatement pst = null;
		int m = 0;
		Connection conn = DBUtils.createConnection();
		try {
			pst = conn.prepareStatement(sql);
			pst.setString(1, UUID.randomUUID().toString());
			pst.setString(2, order.getU_id());
			pst.setString(3, order.getB_id());
			pst.setInt(4, order.getCount());
			pst.setDate(5, new java.sql.Date(order.getOrderTime().getTime()));
			m = pst.executeUpdate();
		} catch (SQLException e) {
			logger.error(e);
		}finally{
			DBUtils.close(conn, pst, null);
		}
		return m;
	}

	@Override
	public int addBatch(List<Order> orders) {
		String sql = "insert into orders values(?,?,?,?,?)";
		PreparedStatement pst = null;
		Connection conn = DBUtils.createConnection();
		try {
			conn.setAutoCommit(false);
			pst = conn.prepareStatement(sql);
			for (Order order : orders) {
				pst.setString(1, UUID.randomUUID().toString());
				pst.setString(2, order.getU_id());
				pst.setString(3, order.getB_id());
				pst.setInt(4, order.getCount());
				pst.setDate(5, new java.sql.Date(order.getOrderTime().getTime()));
				pst.addBatch();
			}
			pst.executeBatch();
			conn.commit();
		} catch (SQLException e) {
			logger.error(e);
			try {
				conn.rollback();
			} catch (SQLException e1) {
				logger.error(e);
			}
			return -1;
		}finally{
			try {
				conn.setAutoCommit(true);
			} catch (SQLException e) {
				logger.error(e);
			}
			DBUtils.close(conn, pst, null);
		}
		return 1;
	}
	
	@Override
	public int delete(String id) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int update(Order t) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Order load(String id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Order> findAll() {
		String sql = "select * from orders";
		Connection conn = DBUtils.createConnection();
		PreparedStatement pst = null;
		ResultSet rs = null;
		List<Order> orders = new ArrayList<>(); 
		try {
			pst = conn.prepareStatement(sql);
			rs = pst.executeQuery();
			while(rs.next()) {
				Order order = new Order();
				order.setId(rs.getString("id"));
				order.setU_id(rs.getString("u_id"));
				order.setB_id(rs.getString("b_id"));
				order.setCount(rs.getInt("count"));
				order.setOrderTime(new Date(rs.getDate("orderTime").getTime()));
				orders.add(order);
			}
		} catch (Exception e) {
			logger.error(e);
		}finally{
			DBUtils.close(conn, pst, rs);
		}
		return orders;	
	}

	
}
